package com.expertria.glex.state;

import com.expertria.glex.event.Events.EventDispatcherHelper;
import com.expertria.glex.event.Events.IEventListener;
import com.expertria.glex.state.StateChangeEvent.STATE;

 

public abstract class StateHelper {

	
	private String currentState ;
	
	private EventDispatcherHelper stateChangeDispatcher = 
		new EventDispatcherHelper();
	
	public void enterState(String state)
	{
		if(this.currentState == null || !this.currentState.equals(state))
		{
			StateChangeEvent stateChangeEvent = 
				new StateChangeEvent(this.currentState, state, this);
		
			this.currentState = state;
			this.stateChangeDispatcher.dispatch( stateChangeEvent );
			onStateChangeImpt(stateChangeEvent.getOldState(), 
					stateChangeEvent.getNewState());
		}
	}
	
	public String getCurrentState() {
		return currentState;
	}
	public void setCurrentState(String currentState) {
		this.currentState = currentState;
	}
	
	public void addStateChangeListener(IEventListener e)
	{
		stateChangeDispatcher.addEventListener(STATE.CHANGE, e);
	}
	
	public abstract void onStateChangeImpt(String oldState, String newState);
}
